package com.example.blogServer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.blogServer.exception.ErrorUsernameOrPassword;
import com.example.blogServer.mapper.UserMapper;
import com.example.blogServer.pojo.dto.UserLoginDTO;
import com.example.blogServer.pojo.entity.User;
import com.example.blogServer.pojo.vo.UserLoginVO;
import com.example.blogServer.service.UserService;
import org.springframework.stereotype.Service;

/**
 * @author Spark
 * @version 1.0
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {

    /**
     * 检查登录,返回合法用户token
     * @param userLoginDTO
     * @return
     */
    @Override
    public UserLoginVO UserLogin(UserLoginDTO userLoginDTO) {

        User user = lambdaQuery()
                .eq(User::getUsername, userLoginDTO.getUsername())
                .eq(User::getPassword, userLoginDTO.getPassword())
                .one();

        //没有查找到就返回错误信息
        if(user == null){
            throw new ErrorUsernameOrPassword("用户名或密码错误");
        }

        //分装成userLoginVO返回
        UserLoginVO userLoginVO = BeanUtil.copyProperties(user, UserLoginVO.class);
        //todo
        userLoginVO.setToken("1");
        return userLoginVO;
    }
}
